{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "whjsJasuhstV"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/app_generative_ai/blob/main/t81_559_class_01_1_overview.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "euOZxlIMhstX"
   },
   "source": [
    "# T81-559: Applications of Generative Artificial Intelligence\n",
    "**Module 1: Course Overview**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-559/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d4Yov72PhstY"
   },
   "source": [
    "# Module 1 Material\n",
    "\n",
    "* **Part 1.1: Course Overview** [[Video]](https://www.youtube.com/watch?v=OVS-6s20Ms0) [[Notebook]](t81_559_class_01_1_overview.ipynb)\n",
    "* Part 1.2: Generative AI Overview [[Video]](https://www.youtube.com/watch?v=ohmPaSsKhMs) [[Notebook]](t81_559_class_01_2_genai.ipynb)\n",
    "* Part 1.3: Introduction to OpenAI [[Video]](https://www.youtube.com/watch?v=C2xyi2Cq-bU) [[Notebook]](t81_559_class_01_3_openai.ipynb)\n",
    "* Part 1.4: Introduction to LangChain [[Video]](https://www.youtube.com/watch?v=qQI5AhaKxuI) [[Notebook]](t81_559_class_01_4_langchain.ipynb)\n",
    "* Part 1.5: Prompt Engineering [[Video]](https://www.youtube.com/watch?v=_Uot1i5sIXo) [[Notebook]](t81_559_class_01_5_prompt_engineering.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "AcAUP0c3hstY"
   },
   "source": [
    "# Google CoLab Instructions\n",
    "\n",
    "The following code ensures that Google CoLab is running and maps Google Drive if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xsI496h5hstZ",
    "outputId": "51ec5398-1a44-4efe-94cf-799426cd643e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: using Google CoLab\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    from google.colab import drive\n",
    "    COLAB = True\n",
    "    print(\"Note: using Google CoLab\")\n",
    "except:\n",
    "    print(\"Note: not using Google CoLab\")\n",
    "    COLAB = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pC9A-LaYhsta"
   },
   "source": [
    "# Part 1.1: Overview\n",
    "\n",
    "In this course, we will learn how to apply Generative Artificial. Intelligence (GenAI). We will focus primarily on text-to-text (such as ChatGPT) and text-to-image (such as Stable Diffusion) models. As you work through this course, you will run models both on your computer as an edge device and in the cloud through an API. To complete this course, it is necessary to purchase cloud computing credits from OpenAI.\n",
    "\n",
    "Generative AI refers to a subset of artificial intelligence (AI) technologies that can generate new content, including but not limited to text, images, music, and even synthetic data. At its core, generative AI leverages complex algorithms to understand patterns, styles, or logic from vast datasets. It then uses this understanding to create original output miming the learned material. This technology has many applications, from creating realistic-looking images that don't exist in the real world to composing music, generating human-like text responses, or developing new video game environments. The power of generative AI lies in its ability to replicate existing forms and styles and innovate by blending elements in novel ways, thus pushing the boundaries of creativity and automated content generation.\n",
    "\n",
    "We will see that GenAI is rooted in neural networks, intense neural networks, and deep learning. The large language models (LLMs) we will see in this course utilize neural network transformers. Similarly, the text-to-image models make use of neural network autoencoders. We will use PyTorch in this course when we need to construct a neural network directly.\n",
    "\n",
    "## Origins of Generative AI\n",
    "\n",
    "The origins of generative AI, encompassing text-to-text language models (LLMs) and text-to-image models, trace back to foundational research in machine learning and artificial intelligence. The development of these technologies represents a pivotal shift in how computers can understand, interpret, and generate human-like content.\n",
    "\n",
    "The journey of generative AI began with exploring neural networks, a concept inspired by the human brain's architecture. Researchers aimed to create algorithms to learn from data, make decisions, and generate outputs without explicit programming for every task. This direction led to the development of deep learning techniques in the early 2010s, which significantly advanced the capabilities of AI systems. Deep learning uses multiple layers of neural networks to analyze data, allowing machines to recognize patterns and features with remarkable accuracy.\n",
    "\n",
    "Text-to-text language models, such as GPT (Generative Pre-trained Transformer), emerged from natural language processing (NLP) progress. These models are trained on vast text datasets, learning language structure, nuances, and complexity. LLMs can generate coherent and contextually relevant text based on input prompts by pre-training on a broad corpus and then fine-tuning for specific tasks. This breakthrough was made possible by innovations in model architecture, such as the transformer model introduced in 2017, which improved the ability of machines to handle sequential data like language.\n",
    "\n",
    "Simultaneously, text-to-image models began to gain traction, driven by advancements in generative adversarial networks (GANs) and variational autoencoders (VAEs). These models learn to generate images by understanding the distribution of visual data, allowing them to produce new images that resemble the training data. The introduction of GANs by Ian Goodfellow and his colleagues in 2014 marked a significant milestone, enabling more realistic and detailed image generation. This technology has evolved to power models like DALL-E, which can create images from textual descriptions, blending visual art and natural language understanding.\n",
    "\n",
    "Generative AI's development reflects a collaborative effort across machine learning, computer vision, and natural language processing. It signifies a move towards creating AI that can interpret the world and generate novel content, offering vast applications in areas ranging from creative arts to automated text generation. As generative AI continues to evolve, it promises to redefine the boundaries of human and machine collaboration, making it a critical area of study and exploration in artificial intelligence.\n",
    "\n",
    "## What is Generative AI\n",
    "\n",
    "Generative AI refers to a subset of artificial intelligence technologies and algorithms that can generate new content, such as text, images, music, and even video, which resemble human-like creations. At its core, Generative AI leverages the power of machine learning and deep learning to analyze and learn from vast amounts of data, identifying patterns, styles, or structures inherent in the data. It then uses this learned information to produce new, original content that has never been seen before but is similar to the data its developers trained the model on.\n",
    "\n",
    "We can trace the roots of Generative AI to the advancements in deep learning, a subset of machine learning that employs neural networks with many layers (hence, \"deep\"). Deep learning models can learn representations of data at multiple abstraction levels, enabling them to understand complex structures within data that their developers trained them on. This ability is crucial for the generative tasks that Generative AI performs.\n",
    "\n",
    "Generative Adversarial Networks (GANs) and Variational Autoencoders (VAEs) are two of the most significant breakthroughs in Generative AI, which are based on deep learning principles.\n",
    "Generative Adversarial Networks (GANs): Introduced by Ian Goodfellow and his colleagues in 2014, GANs consist of two neural networks—the generator and the discriminator—trained simultaneously in a competitive setting. The generator learns to produce indistinguishable content from real data, while the discriminator learns to differentiate between real and generated content. This adversarial process enhances the quality of the generated content over time.\n",
    "\n",
    "Variational Autoencoders (VAEs): VAEs are another pivotal architecture in the field of Generative AI. They are designed to encode input data into a lower-dimensional space and then reconstruct it back to the original form. Through this process, VAEs learn to capture the essential characteristics of the data, allowing them to generate new data points with similar properties.\n",
    "\n",
    "The development of Generative AI has been propelled by the increasing computational power available and the vast amounts of data for training. This development has led to its application across various fields, from creating realistic images and art, composing music, generating realistic human voices, and creating synthetic data for training other AI models to even drug discovery and material science.\n",
    "\n",
    "Generative AI's ability to create novel content opens up many possibilities for innovation across creative industries, technology, and science. It represents a frontier in AI where machines are not just tools for automation or decision-making but are also partners in creativity, offering new ways to inspire and accelerate human invention.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "VuXAE2fthstb"
   },
   "source": [
    "# Python for GenAI\n",
    "\n",
    "We will utilize the Python 3.x programming language for this book. Python has some of the widest support for deep learning as a programming language. The two most popular frameworks for deep learning in Python are:\n",
    "\n",
    "* [TensorFlow/Keras](https://www.tensorflow.org/) (Google)\n",
    "* [PyTorch](https://pytorch.org/) (Facebook)\n",
    "\n",
    "\n",
    "This book focuses primarily upon PyTorch for GenAI. For many tasks, we will utilize PyTorch directly. However, we will extensivly utilize the OpenAI API, as well as other third-party libraries for higher-level tasks. These third-party libraries may internally make use of either PyTorch or even Keras. I chose these libraries based on popularity and application, not whether they used PyTorch or Keras.\n",
    "\n",
    "To successfully use this book, you must be able to compile and execute Python code that makes use of PyTorch for deep learning. There are two options for you to accomplish this:\n",
    "\n",
    "* Install Python, PyTorch and some IDE (Jupyter, TensorFlow, and others).\n",
    "* Use [Google CoLab](https://colab.research.google.com/) in the cloud, with free GPU access.\n",
    "\n",
    "If you look at this notebook on Github, near the top of the document, there are links to videos that describe how to use Google CoLab. There are also videos explaining how to install Python on your local computer. The following sections take you through the process of installing Python on your local computer. This process is essentially the same on Windows, Linux, or Mac. For specific OS instructions, refer to one of the tutorial YouTube videos earlier in this document.\n",
    "\n",
    "## Tokens and Keys\n",
    "\n",
    "You will have several tokens and keys for this class.\n",
    "\n",
    "* Homework Submission API-Key - You will be emailed an API-Key prior to class; you will make use of this key to submit assignments.\n",
    "* Hugging Face Key - You will need to log into [HuggingFace](https://huggingface.co/) and obtain a free key to download certain pretrained models.\n",
    "* OpenAI Key - Students must obtain an OpenAI key and link a credit card. Expect to spend approximatly $100 USD to complete this course.\n",
    "\n",
    "## Check your Python Installation\n",
    "\n",
    "Once you've installed Python, you can utilize the following code to check your Python and library versions. If you have a GPU, you can also check to see that Keras recognize it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "okZQe6lzhstb",
    "outputId": "fe080838-ea48-4071-b6e0-4efe359758f7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python Platform: Linux-6.1.58+-x86_64-with-glibc2.35\n",
      "PyTorch Version: 2.2.1+cu121\n",
      "\n",
      "Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]\n",
      "Pandas 2.0.3\n",
      "Scikit-Learn 1.2.2\n",
      "GPU is NOT AVAILABLE\n",
      "MPS (Apple Metal) is NOT AVAILABLE\n",
      "Target device is cpu\n"
     ]
    }
   ],
   "source": [
    "# What version of Python do you have?\n",
    "import platform\n",
    "import sys\n",
    "\n",
    "import pandas as pd\n",
    "import sklearn as sk\n",
    "import torch\n",
    "\n",
    "has_gpu = torch.cuda.is_available()\n",
    "has_mps = torch.backends.mps.is_built()\n",
    "device = \"mps\" if has_mps else \"cuda\" if has_gpu else \"cpu\"\n",
    "\n",
    "print(f\"Python Platform: {platform.platform()}\")\n",
    "print(f\"PyTorch Version: {torch.__version__}\")\n",
    "print()\n",
    "print(f\"Python {sys.version}\")\n",
    "print(f\"Pandas {pd.__version__}\")\n",
    "print(f\"Scikit-Learn {sk.__version__}\")\n",
    "print(\"GPU is\", \"available\" if has_gpu else \"NOT AVAILABLE\")\n",
    "print(\"MPS (Apple Metal) is\", \"AVAILABLE\" if has_mps else \"NOT AVAILABLE\")\n",
    "print(f\"Target device is {device}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "GR-0hiBlhstb"
   },
   "source": [
    "# Module 1 Assignment\n",
    "\n",
    "You can find the first assignment here: [assignment 1](https://github.com/jeffheaton/app_deep_learning/blob/main/assignments/assignment_yourname_class1.ipynb)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3.11 (genai)",
   "language": "python",
   "name": "genai"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
